/**
 *  Created on  : 06/06/2012
 *  Author      : Ing. Javier Silva Pérez - [javier]
 *  Description :
 *  	Wraps the information of a Elliptic Curve Private Key and contain some parse functions 
 *  	to interact with JCE and SpongyCastle EC privateKey representations
 */
package cinvestav.cryptography.pki.android.key;

import java.math.BigInteger;
import java.security.spec.ECPrivateKeySpec;

import org.spongycastle.crypto.params.ECPrivateKeyParameters;

import cinvestav.cryptography.pki.android.ec.ECDomainParameters;
import cinvestav.cryptography.pki.android.exception.AndroidCryptoUtilsException;

/**
 * Wraps the information of a Elliptic Curve Private Key and contain some parse
 * functions to interact with JCE and SpongyCastle EC privateKey representations
 * 
 * @author Ing. Javier Silva Pérez - [javier]
 * @since 06/06/2012
 * @version 1.0
 */
public class ECPrivateKey {

	ECDomainParameters params;
	BigInteger d;

	/**
	 * Copy constructor
	 * 
	 * @param privateKey
	 *            Source privateKey
	 */
	public ECPrivateKey(ECPrivateKey privateKey) {
		this.params = privateKey.getParams();
		this.d = privateKey.getD();
	}

	/**
	 * Default parameterized constructor
	 * 
	 * @param params
	 *            Parameters of the curve
	 * @param d
	 *            BigInteger that represents the privateKey
	 */
	public ECPrivateKey(ECDomainParameters params, BigInteger d) {
		super();
		this.params = params;
		this.d = d;
	}

	public ECDomainParameters getParams() {
		return params;
	}

	public void setParams(ECDomainParameters params) {
		this.params = params;
	}

	public BigInteger getD() {
		return d;
	}

	public void setD(BigInteger d) {
		this.d = d;
	}

	@Override
	public String toString() {
		return "ECPrivateKey [params=" + params + ", d=" + d + "]";
	}

	/**
	 * Parse an object of type ECPrivateKeyParameters (
	 * {@link org.spongycastle.crypto.params.ECPrivateKeyParameter}) to this
	 * class
	 * 
	 * @param privateKey
	 *            ECPrivateKeyParameters Private key object
	 * @return A new ECPrivateKey object with the parsed values;
	 * @throws AndroidCryptoUtilsException
	 */
	public static ECPrivateKey parse(ECPrivateKeyParameters privateKey)
			throws AndroidCryptoUtilsException {
		ECPrivateKey privateKeyRes = new ECPrivateKey(
				ECDomainParameters.parse(privateKey.getParameters()),
				privateKey.getD());
		return privateKeyRes;
	}

	/**
	 * Parse the calling object to ECPrivateKeyParameters type (
	 * {@link org.spongycastle.crypto.params.ECPrivateKeyParameter})
	 * 
	 * @return A ECPrivateKeyParameters with the values of the calling object
	 */
	public ECPrivateKeyParameters parseToECPrivateKeyParameters() {
		ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters(this.d,
				params.parseToECDomainParameters());
		return privateKey;
	}

	/**
	 * Parse a JCE ECPrivateKey {@link java.security.interfaces.ECPrivateKey})
	 * to this class, the result will be in the calling object
	 * 
	 * @param privateKey
	 *            JCE ECPrivateKey object
	 * @throws AndroidCryptoUtilsException
	 */
	/*
	 * public void parse(java.security.interfaces.ECPrivateKey privateKey)
	 * throws AndroidCryptoUtilsException { this.d = privateKey.getS();
	 * this.params = ECDomainParameters.parse(privateKey.getParams()); }
	 * 
	 * public java.security.interfaces.ECPrivateKey parseToJCEECPrivateKey(){
	 * java.security.interfaces.ECPrivateKey privateKey = new
	 * java.security.interfaces.ECPrivateKey }
	 */

	/**
	 * Parse the JCE ECPrivateKeySpec (
	 * {@link java.security.spec.ECPrivateKeySpec})
	 * 
	 * @param privateKey
	 *            JCE ECPrivateKeySpec object
	 * @return A new ECPrivateKey object with the values of the passed
	 *         privateKey
	 * @throws AndroidCryptoUtilsException
	 */
	public static ECPrivateKey parse(ECPrivateKeySpec privateKey)
			throws AndroidCryptoUtilsException {
		ECPrivateKey privateKeyRes = new ECPrivateKey(
				ECDomainParameters.parse(privateKey.getParams()),
				privateKey.getS());
		return privateKeyRes;
	}

	/**
	 * Parse the calling object to JCE ECPrivateKeySpec type (
	 * {@link java.security.spec.ECPrivateKeySpec})
	 * 
	 * @return A ECPrivateKeySpec with the values of the calling object
	 * @throws AndroidCryptoUtilsException
	 */
	public ECPrivateKeySpec parseToECPrivateKeySpec()
			throws AndroidCryptoUtilsException {
		ECPrivateKeySpec privateKey = new ECPrivateKeySpec(this.d,
				params.parseToJCEECParameterSpec());
		return privateKey;
	}

	/**
	 * Parse the SpongyCastle ECPrivateKeySpec (
	 * {@link org.spongycastle.jce.spec.ECPrivateKeySpec})
	 * 
	 * @param privateKey
	 *            SpongyCastle ECPrivateKeySpec object
	 * @return A new ECPrivateKey object with the values of the passed
	 *         privateKey
	 * @throws AndroidCryptoUtilsException
	 */
	public static ECPrivateKey parse(
			org.spongycastle.jce.spec.ECPrivateKeySpec privateKey)
			throws AndroidCryptoUtilsException {
		ECPrivateKey privateKeyRes = new ECPrivateKey(
				ECDomainParameters.parse(privateKey.getParams()),
				privateKey.getD());
		return privateKeyRes;
	}

	/**
	 * Parse the calling object to SpongyCastle ECPrivateKeySpec type (
	 * {@link org.spongycastle.jce.spec.ECPrivateKeySpec})
	 * 
	 * @return A SC ECPrivateKeySpec with the values of the calling object
	 */
	public org.spongycastle.jce.spec.ECPrivateKeySpec parseToSCECPrivateKeySpec() {
		org.spongycastle.jce.spec.ECPrivateKeySpec privateKey = new org.spongycastle.jce.spec.ECPrivateKeySpec(
				this.d, params.parseToECParameterSpec());
		return privateKey;
	}
}
